package dahuiji.common.filter;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import dahuiji.common.util.EncodingFormat;
import dahuiji.common.util.HtmlUtil;

/**
 * Servlet Filter implementation class CommonFilter
 */
public class CommonFilter implements Filter {

	/**
	 * Default constructor.
	 */
	public CommonFilter() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		// TODO Auto-generated method stub
		// place your code here
		// pass the request along the filter chain
		System.out.println("----------------过滤器-------------------------");
		request.setCharacterEncoding("UTF-8");
		HashMap m = new HashMap(request.getParameterMap());
		Map params = request.getParameterMap();
		Iterator iter = params.entrySet().iterator();
		while (iter.hasNext()) {
			Map.Entry entry = (Map.Entry) iter.next();
			Object key = entry.getKey();
			String[] values = ((String[]) entry.getValue());
			for (int i = 0; i < values.length; i++) {
				String value = HtmlUtil.convertTag2CC(values[i]);
				//value=EncodingFormat.iso2Utf(value);
				values[i] = value;
			}
			m.put(key, values);
		}
		chain.doFilter(request, response);
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
	}

}
